﻿using ExDUIR.NET.Color;
using ExDUIR.NET.Declaration;
using ExDUIR.NET.Handle;
using ExDUIR.NET.Canvas;
using ExDUIR.NET.Matrix;
using ExDUIR.NET.Image;

namespace ExDUIR.NET.Brush
{
    public class ExBrush
    {
        private readonly ExBrushHandle _handle;
        public bool IsValid => _handle.IsInvalid;
        public IntPtr Handle => _handle.Handle;
        private ExARGB _color;

        internal ExBrush(ExBrushHandle handle)
        {
            _handle = handle;
        }

        internal ExBrush(ExARGB Color)
        {
            _color = Color;
            _handle = new ExBrushHandle(ExAPI._brush_create(Color));
        }

        internal ExBrush(ExCanvas handle)
        {
            _handle = new ExBrushHandle(ExAPI._brush_createfromcanvas(handle.Handle));
        }

        /// <summary>
        /// 创建线性渐变画刷
        /// </summary>
        /// <param name="xStart"></param>
        /// <param name="yStart"></param>
        /// <param name="xEnd"></param>
        /// <param name="yEnd"></param>
        /// <param name="crBegin">起点颜色ARGB</param>
        /// <param name="crEnd">终点颜色ARGB</param>
        internal ExBrush(int xStart, int yStart, int xEnd, int yEnd, int crBegin, int crEnd)
        {
            _handle = new ExBrushHandle(ExAPI._brush_createlinear(xStart, yStart, xEnd, yEnd, crBegin, crEnd));
        }

        /// <summary>
        /// 创建线性渐变画刷2
        /// </summary>
        /// <param name="xStart">起点X</param>
        /// <param name="yStart">起点Y</param>
        /// <param name="xEnd">终点X</param>
        /// <param name="yEnd">终点Y</param>
        /// <param name="arrPts">位置数组(取值0-1.0),两个</param>
        /// <param name="arrColors">ARGB颜色数组,两个</param>
        internal ExBrush(int xStart, int yStart, int xEnd, int yEnd, float[] arrPts, int[] arrColors)
        {
            _handle = new ExBrushHandle(ExAPI._brush_createlinear_ex(xStart, yStart, xEnd, yEnd, arrPts, arrColors));
        }

        internal ExBrush(ExImage img)
        {
            _handle = new ExBrushHandle(ExAPI._brush_createfromimg(img.Handle));
        }

        public int Color
        {
            get
            {
                return _color;
            }
            set
            {
                ExAPI._brush_setcolor(_color, value);
            }
        }

        public ExMatrix TransFrom
        {
            set
            {
                ExAPI._brush_settransform(_handle.Handle, value.Handle);
            }
        }

        public void Dispose() => _handle.Dispose();
    }
}
